﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER.Manual
{
    public partial class UC_InspCtr : UserControl
    {
        public UC_InspCtr()
        {
            InitializeComponent();
            GetControls(this.Controls);
        }

        private int InspIndex = 0;
        private string Name = "检测CV1";

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                //if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                //{
                //    ((Button)item).OP = Kernel.OP;
                //}

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                #region  CV控制
                case "InspCVRun":
                    {
                        InspCVCtr(true,InspIndex);                        
                    }
                    break;
                case "InspCVStop":
                    {
                        InspCVCtr(false, InspIndex);
                    }
                    break;
                #endregion

                #region CV定位Y轴控制
                case "InspCV_YWork":
                    {
                        InspCVLocateControl(2, InspIndex);
                    }
                    break;
                case "InspCV_YWait":
                    {
                        InspCVLocateControl(1, InspIndex);
                    }
                    break;
                #endregion

                #region  气缸控制
                case "InspCyUp":
                    {
                        Cyl_Ctr_Up(InspIndex);
                    }
                    break;
                case "InspCyDn":
                    {
                        Cyl_Ctr_Dn(InspIndex);
                    }
                    break;
                #endregion

                #region Z轴力控

                case "Insp_RM_Work":
                    {
                        Insp_RMControl(1, InspIndex);
                    }
                    break;
                case "Insp_RM_Wait":
                    {
                        Insp_RMControl(2, InspIndex);
                    }
                    break;
                #endregion

                #region 真空控制


                case "VacOn":
                    {                       
                        InspVacCtr(true, InspIndex);
                    }
                    break;
                case "VacBreak":
                    {
                        InspVacCtr(false, InspIndex);
                    }
                    break;


                    #endregion
            }
        }
        /// <summary>
        /// 真空控制
        /// </summary>
        /// <param name="ctr">true 吸真空/false 破真空</param>
        /// <param name="inspIndex">检测平台Index</param>
        /// <exception cref="NotImplementedException"></exception>
        private void InspVacCtr(bool ctr, int inspIndex)
        {
            if (ctr)
            {
                if (inspIndex == 1)
                {
                    ProcedureHelper_Exc.Insp1_ToVacOnn = true;
                }
                else if (inspIndex == 2)
                {
                    ProcedureHelper_Exc.Insp2_ToVacOnn = true;
                }
                else if (inspIndex == 3)
                {
                    ProcedureHelper_Exc.Insp3_ToVacOnn = true;
                }
                else if (inspIndex == 4)
                {
                    ProcedureHelper_Exc.Insp4_ToVacOnn = true;
                }
            }
            else
            {
                if (inspIndex == 1)
                {
                    ProcedureHelper_Exc.Insp1_ToVacOff = true;
                }
                else if (inspIndex == 2)
                {
                    ProcedureHelper_Exc.Insp2_ToVacOff = true;
                }
                else if (inspIndex == 3)
                {
                    ProcedureHelper_Exc.Insp3_ToVacOff = true;
                }
                else if (inspIndex == 4)
                {
                    ProcedureHelper_Exc.Insp4_ToVacOff = true;
                }

                //Kernel.Exc.DO.Y033_1_Insp_VacBrk = 0;
                //Kernel.Exc.DO.Y03B_2_Insp_VacBrk = 0;
                //Kernel.Exc.DO.Y043_3_Insp_VacBrk = 0;
                //Kernel.Exc.DO.Y04B_4_Insp_VacBrk = 0;
            }
        }

        private void Insp_RMControl(int v, int inspIndex)
        {

            if (v == 1)
            {
                if (inspIndex == 1)
                {
                    ProcedureHelper_Exc.Insp1_RM_Z_ToWork = true;
                }
                else if (inspIndex == 2)
                {
                    ProcedureHelper_Exc.Insp2_RM_Z_ToWork = true;
                }
                else if (inspIndex == 3)
                {
                    ProcedureHelper_Exc.Insp3_RM_Z_ToWork = true;
                }
                else if (inspIndex == 4)
                {
                    ProcedureHelper_Exc.Insp4_RM_Z_ToWork = true;
                }
            }
            if (v == 2)
            {
                if (inspIndex == 1)
                {
                    ProcedureHelper_Exc.Insp1_RM_Z_ToWait= true;
                }
                else if (inspIndex == 2)
                {
                    ProcedureHelper_Exc.Insp2_RM_Z_ToWait = true;
                }
                else if (inspIndex == 3)
                {
                    ProcedureHelper_Exc.Insp3_RM_Z_ToWait = true;
                }
                else if (inspIndex == 4)
                {
                    ProcedureHelper_Exc.Insp4_RM_Z_ToWait = true;
                }
            }
        }

        private void InspCVCtr(bool run, int inspIndex)
        {
            if (run)
            {
                if (inspIndex == 1)
                {
                    ProcedureHelper_Exc.Insp1CV_ToRun = true;
                }
                else if (inspIndex == 2)
                {
                    ProcedureHelper_Exc.Insp2CV_ToRun = true;
                }
                else if (inspIndex == 3)
                {
                    ProcedureHelper_Exc.Insp3CV_ToRun = true;
                }
                else if (inspIndex == 4)
                {
                    ProcedureHelper_Exc.Insp4CV_ToRun = true;
                }
            }
            else
            {
                if (inspIndex == 1)
                {
                    ProcedureHelper_Exc.Insp1CV_ToStop = true;
                    ProcedureHelper_Exc.Insp1CV_ToRun = false;
                }
                else if (inspIndex == 2)
                {
                    ProcedureHelper_Exc.Insp2CV_ToStop = true;
                }
                else if (inspIndex == 3)
                {
                    ProcedureHelper_Exc.Insp3CV_ToStop = true;
                }
                else if (inspIndex == 4)
                {
                    ProcedureHelper_Exc.Insp4CV_ToStop = true;
                }
            }
        }

        private void Cyl_Ctr_Dn(int inspIndex)
        {
            if (inspIndex == 1)
            {
                ProcedureHelper_Exc.Insp1_Cyl_ToDn = true;
            }
            else if (inspIndex == 2)
            {
                ProcedureHelper_Exc.Insp2_Cyl_ToDn = true;
            }
            else if (inspIndex == 3)
            {
                ProcedureHelper_Exc.Insp3_Cyl_ToDn = true;
            }
            else if (inspIndex == 4)
            {
                ProcedureHelper_Exc.Insp4_Cyl_ToDn = true;

            }
        }

        private void Cyl_Ctr_Up(int inspIndex)
        {
            if (inspIndex == 1)
            {
                ProcedureHelper_Exc.Insp1_Cyl_ToUp = true;
            }
            else if (inspIndex == 2)
            {
                ProcedureHelper_Exc.Insp2_Cyl_ToUp = true;
            }
            else if (inspIndex == 3)
            {
                ProcedureHelper_Exc.Insp3_Cyl_ToUp = true;
            } 
            else if (inspIndex == 4)
            {
                ProcedureHelper_Exc.Insp4_Cyl_ToUp = true;
            }          
        }
        /// <summary>
        /// 定位控制
        /// </summary>
        /// <param name="ctrNo">轴定位编号1-Wait 2-Work</param>
        /// <param name="index">模组编号</param>
        private void InspCVLocateControl(int ctrNo,int index)
        {
            if (ctrNo == 1)
            {
                if (index == 1)
                {
                    ProcedureHelper_Exc.Insp1_Y_ToWait = true;
                }
                else if (index == 2)
                {
                    ProcedureHelper_Exc.Insp2_Y_ToWait = true;
                }
                else if (index == 3)
                {
                    ProcedureHelper_Exc.Insp3_Y_ToWait = true;
                }
                else if (index == 4)
                {
                    ProcedureHelper_Exc.Insp4_Y_ToWait = true;
                }
            }
            else if (ctrNo == 2)
            {
                if (index == 1)
                {
                    ProcedureHelper_Exc.Insp1_Y_ToWork = true;
                }
                else if (index == 2)
                {
                    ProcedureHelper_Exc.Insp2_Y_ToWork = true;
                }
                else if (index == 3)
                {
                    ProcedureHelper_Exc.Insp3_Y_ToWork = true;
                }
                else if (index == 4)
                {
                    ProcedureHelper_Exc.Insp4_Y_ToWork = true;
                }

            }
        }



       public void FromInit(int index,string name)
        { 
            this.InspIndex = index;
            this.Name = name;
        }

        public void Load()
        {
            this.groupBox1.Text = Name;
            timer1.Start();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;
            FreshStatus();
           
        }

        private void FreshStatus()
        {
            if (InspIndex == 1)
            {
                btnInspCVRun.BackColor =  ProcedureHelper_Exc .Insp1CV_Run_BL ? Color.Green : Color.LightGray;
                btn_InspCVStop.BackColor =  ProcedureHelper_Exc.Insp1CV_Stop_BL ? Color.Green : Color.LightGray;
                btn_InspCyUp.BackColor =  ProcedureHelper_Exc.Insp1_Cyl_Up_BL ? Color.Green : Color.LightGray;
                btn_InspCyDn.BackColor = ProcedureHelper_Exc.Insp1_Cyl_Dn_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWait.BackColor =  ProcedureHelper_Exc.Insp1_Y_Wait_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWork.BackColor =  ProcedureHelper_Exc.Insp1_Y_Work_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Wait.BackColor = ProcedureHelper_Exc.Insp1_RM_Z_Wait_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Work.BackColor = ProcedureHelper_Exc.Insp1_RM_Z_Work_BL ? Color.Green : Color.LightGray;
                lbl_Z_TorqValue.Text = Kernel.motion.Rm_Insp1_Z.Torque.ToString("F2") + "N";
                btn_VacOn.BackColor= ProcedureHelper_Exc.Insp1_VacOnn_BL ? Color.Green : Color.LightGray;
                btn_VacBreak.BackColor = ProcedureHelper_Exc.Insp1_VacOff_BL ? Color.Green : Color.LightGray;
            }
            else if (InspIndex == 2)
            {
                btnInspCVRun.BackColor = ProcedureHelper_Exc.Insp2CV_Run_BL ? Color.Green : Color.LightGray;
                btn_InspCVStop.BackColor = ProcedureHelper_Exc.Insp2CV_Stop_BL ? Color.Green : Color.LightGray;
                btn_InspCyUp.BackColor = ProcedureHelper_Exc.Insp2_Cyl_Up_BL ? Color.Green : Color.LightGray;
                btn_InspCyDn.BackColor = ProcedureHelper_Exc.Insp2_Cyl_Dn_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWait.BackColor = ProcedureHelper_Exc.Insp2_Y_Wait_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWork.BackColor = ProcedureHelper_Exc.Insp2_Y_Work_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Wait.BackColor = ProcedureHelper_Exc.Insp2_RM_Z_Wait_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Work.BackColor = ProcedureHelper_Exc.Insp2_RM_Z_Work_BL ? Color.Green : Color.LightGray;
                lbl_Z_TorqValue.Text = Kernel.motion.Rm_Insp2_Z.Torque.ToString("F2")+"N";

                btn_VacOn.BackColor = ProcedureHelper_Exc.Insp2_VacOnn_BL ? Color.Green : Color.LightGray;
                btn_VacBreak.BackColor = ProcedureHelper_Exc.Insp2_VacOff_BL ? Color.Green : Color.LightGray;
            }
            else if (InspIndex == 3)
            {
                btnInspCVRun.BackColor = ProcedureHelper_Exc.Insp3CV_Run_BL ? Color.Green : Color.LightGray;
                btn_InspCVStop.BackColor = ProcedureHelper_Exc.Insp3CV_Stop_BL ? Color.Green : Color.LightGray;
                btn_InspCyUp.BackColor = ProcedureHelper_Exc.Insp3_Cyl_Up_BL ? Color.Green : Color.LightGray;
                btn_InspCyDn.BackColor = ProcedureHelper_Exc.Insp3_Cyl_Dn_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWait.BackColor = ProcedureHelper_Exc.Insp3_Y_Wait_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWork.BackColor = ProcedureHelper_Exc.Insp3_Y_Work_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Wait.BackColor = ProcedureHelper_Exc.Insp3_RM_Z_Wait_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Work.BackColor = ProcedureHelper_Exc.Insp3_RM_Z_Work_BL ? Color.Green : Color.LightGray;
                lbl_Z_TorqValue.Text = Kernel.motion.Rm_Insp3_Z.Torque.ToString("F2") + "N";

                btn_VacOn.BackColor = ProcedureHelper_Exc.Insp3_VacOnn_BL ? Color.Green : Color.LightGray;
                btn_VacBreak.BackColor = ProcedureHelper_Exc.Insp3_VacOff_BL ? Color.Green : Color.LightGray;
            }
            else if (InspIndex == 4)
            {
                btnInspCVRun.BackColor = ProcedureHelper_Exc.Insp4CV_Run_BL ? Color.Green : Color.LightGray;
                btn_InspCVStop.BackColor = ProcedureHelper_Exc.Insp4CV_Stop_BL ? Color.Green : Color.LightGray;
                btn_InspCyUp.BackColor = ProcedureHelper_Exc.Insp4_Cyl_Up_BL ? Color.Green : Color.LightGray;
                btn_InspCyDn.BackColor = ProcedureHelper_Exc.Insp4_Cyl_Dn_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWait.BackColor = ProcedureHelper_Exc.Insp4_Y_Wait_BL ? Color.Green : Color.LightGray;
                btn_InspCV_YWork.BackColor = ProcedureHelper_Exc.Insp4_Y_Work_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Wait.BackColor = ProcedureHelper_Exc.Insp4_RM_Z_Wait_BL ? Color.Green : Color.LightGray;
                btnInsp_RM_Work.BackColor = ProcedureHelper_Exc.Insp4_RM_Z_Work_BL ? Color.Green : Color.LightGray;
                lbl_Z_TorqValue.Text = Kernel.motion.Rm_Insp4_Z.Torque.ToString("F2") + "N";

                btn_VacOn.BackColor = ProcedureHelper_Exc.Insp4_VacOnn_BL ? Color.Green : Color.LightGray;
                btn_VacBreak.BackColor = ProcedureHelper_Exc.Insp4_VacOff_BL ? Color.Green : Color.LightGray;

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
